package com.gitee.fanx.retty.demo.common;

import com.gitee.fanx.retty.web.Request;
import com.gitee.fanx.retty.web.ResponseHandler;
import com.gitee.fanx.retty.web.Route;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 统一Web响应
 *
 * @author fanhang
 */
@Component
public class WebResponseBodyHandler implements ResponseHandler {

	private Logger log = LoggerFactory.getLogger(WebResponseBodyHandler.class);

	@Override
	public Object handle(Request request, Route route, Object body) {
		if (body == null) {
			log.debug("{} response null", route);
			return WebResult.success();
		}
		WebResult<?> result = null;
		if (body instanceof WebResult) {
			result = (WebResult<?>) body;
		} else {
			result = WebResult.success(body);
		}
		log.debug("{} response body: {}", route, body);
		return result;
	}
}
